package com.ruoyi.web.controller.community.service;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.community.domain.ServiceRules;
import com.ruoyi.community.service.IRulesService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/community/service/rules")
public class ServiceRuleController {

    private String baseImgPath = "C:\\imgTest\\";
//    private String baseImgPath = "/project/smart-community/pictures/";


    @Autowired
    private IRulesService rulesService;

    @GetMapping("/")
    public AjaxResult getRulesList() {
        Map<String, List<ServiceRules>> rulesMap = new HashMap<>();
        rulesMap = rulesService.getRulesList();
        return AjaxResult.success("查询成功！", rulesMap);
    }

    @PostMapping("/add")
    public AjaxResult addNewRule(@RequestBody ServiceRules serviceRules) {
        System.out.println("请求参数" + serviceRules);
        if(serviceRules != null) {
            Integer result = rulesService.addNewRule(serviceRules);
            if(result > 0) {
                return AjaxResult.success("添加成功！");
            } else {
                return AjaxResult.success("向数据库中添加失败！");
            }
        } else {
            return AjaxResult.success("请求参数有问题，添加失败！");
        }
    }

    @PutMapping("/")
    public AjaxResult updateRuleInfo(@RequestBody ServiceRules serviceRules) {
        if(!serviceRules.getType().isEmpty() &&
                !serviceRules.getBrief().isEmpty() &&
                !serviceRules.getContent().isEmpty() &&
                !serviceRules.getCoverUrl().isEmpty()) {
            Integer result = rulesService.updateRuleInfoById(serviceRules);
            if(result > 0) {
                return AjaxResult.success("信息更新成功！");
            } else {
                return AjaxResult.error("信息更新失败！");
            }
        } else {
            return AjaxResult.error("信息不能为空，请检查后重新更新！");
        }
    }

    @DeleteMapping("/{ruleId}")
    public AjaxResult deleteRuleById(@PathVariable(value = "ruleId") Long ruleId) {
        if(ruleId > 0) {
            Integer result = rulesService.deleteRuleById(ruleId);
            if(result > 0) {
                return AjaxResult.success("删除成功！");
            }
        }
        return AjaxResult.error("不合法");
    }

    @PostMapping("/uploadFile")
    public AjaxResult addRuleCover(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("开始保存图片");
        if(file != null){
            String path = null;// 文件路径
            String imgType = null;//图片类型
            String  fileName = file.getOriginalFilename();// 原文件名称
            System.out.println(fileName);
            imgType = fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
            if(imgType!=null){
                if("GIF".equals(imgType.toUpperCase()) || "PNG".equals(imgType.toUpperCase()) || "JPG".equals(imgType.toUpperCase())){
                    // 项目在容器中实际发布运行的根路径
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + "_" +  fileName;
                    // 设置图片存放的路径
                    path = baseImgPath + trueFileName;
                    // 转存文件到指定路径
                    file.transferTo(new File(path)); // 转存而不是写出
                    System.out.println(AjaxResult.success("上传成功", trueFileName));
                    return AjaxResult.success("上传成功", trueFileName);
                }
            }
        } else {
            return AjaxResult.error("上传失败");
        }
        return null;
    }


/*小程序上传图片*/
    @PostMapping("/uploadFile1")
    public String addRuleCover1(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("开始保存图片");
        if(file != null){
            String path = null;// 文件路径
            String imgType = null;//图片类型
            String  fileName = file.getOriginalFilename();// 原文件名称
            //System.out.println(fileName);
            imgType = fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
            if(imgType!=null){
                if("GIF".equals(imgType.toUpperCase()) || "PNG".equals(imgType.toUpperCase()) || "jpeg".equals(imgType.toUpperCase()) || "JPG".equals(imgType.toUpperCase())){
                    // 项目在容器中实际发布运行的根路径
                    // 自定义的文件名称
                    String trueFileName = String.valueOf(System.currentTimeMillis()) + "_" +  fileName;
                    // 设置图片存放的路径
                    path="/Users/littlebear/Desktop/zhds/upload/" + trueFileName;
                    // 转存文件到指定路径
                    file.transferTo(new File(path)); // 转存而不是写出
                    System.out.println(AjaxResult.success("上传成功", trueFileName));
                    return path;
                    //return AjaxResult.success("上传成功", trueFileName);
                }
            }
        } else {
            return "图片为空，上传失败";
        }
        return null;
    }

    @DeleteMapping("/deleteImg")
    public AjaxResult deleteRuleCover(@RequestParam("fileName") String fileName) {
        File file = new File(baseImgPath + fileName);
        System.out.println(baseImgPath + fileName);
        if(file.exists()) {
            file.delete();
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    @GetMapping("/{ruleId}")
    public AjaxResult getRuleById(@PathVariable Long ruleId) {
        ServiceRules rule = rulesService.getRuleById(ruleId);
        if(rule != null) {
            return AjaxResult.success("查询成功！", rule);
        }
        return AjaxResult.success("查询失败！");
    }
}
